<?php

/**
 * uniBoard ACP New Task
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_task{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_task( $appCore);
					
		// Update?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Fill Api
			$this -> api -> setTaskName( $appCore -> _POST( 'task_name'));
			$this -> api -> setTaskDescription( $appCore -> _POST( 'task_description'));
			$this -> api -> setTaskActive( $appCore -> _POST( 'task_active'));
			$this -> api -> setTaskDays( $appCore -> _POST( 'task_days'));
			$this -> api -> setTaskHours( $appCore -> _POST( 'task_hours'));
			$this -> api -> setTaskMinutes( $appCore -> _POST( 'task_minutes'));
			
			// File incorrect?
			$task_file = $appCore -> _POST( 'task_file');
			
			// Extract file extension
			$task_file_ext = strtolower( substr( $task_file, strrpos( $task_file, '.')));
			
			// Extension is incorrect?
			if ( $task_file_ext != PHP_EXT)
			{
				$this -> api -> setError( $appCore -> lang['acp_task_file_incorrect']);
			}
			
			// Get File name
			$task_file = $appCore -> secureFileName( substr( $task_file, 0, strrpos( $task_file, '.'))) . $task_file_ext;
			
			// File doesnt exists?
			if ( !file_exists( TASKS_PATH . $task_file))
			{
				$this -> api -> setError( $appCore -> lang['acp_task_file_incorrect']);
			}
			
			// Api All-rite?
			if ( $this -> api -> error)
			{
				// Draw Errors
				$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['new_task_form']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Set Task File
				$this -> api -> setTaskFile( $task_file);
			
				// Save Api
				$this -> api -> saveData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_created_task', htmlspecialchars( $appCore -> _POST( 'task_name')));
				
				// Redirect us back to list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=tasks'), $appCore -> lang['acp_task_added']);
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_task'));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_task_form_sect_name']);
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_task_form_name'], $this-> appCore -> drawTextInput( 'task_name', $this -> api -> task_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_task_form_description'], $this-> appCore -> drawTextArea( 'task_description', $this -> api -> task_description));
		
		// Next Section
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_task_form_sect_file']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_task_form_active'], $this-> appCore -> drawYesNo( 'task_active', $this -> api -> task_active));
		
		// Use Glob?
		if ( USE_GLOB)
		{
			// Grab files
			$task_files = glob( TASKS_PATH . '*.php');
			
			// Reiterate them
			foreach ( $task_files as $file_id => $file_raw)
			{
				// Fix raw
				$file_raw = substr( $file_raw, strrpos( $file_raw, '/') + 1);
				
				// Put to array
				$task_files[$file_raw] = $file_raw;
				
				// Unset our item
				unset( $task_files[$file_id]);
			}
			
			// Fancy list	
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_task_form_file'], $this-> appCore -> drawList( 'task_file', $task_files, $this -> api -> task_file), sprintf( $this -> appCore -> lang['acp_task_form_file_expl'], TASKS_PATH));
		
		}
		else
		{
			// Dumb "goaround"
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_task_form_file'], $this-> appCore -> drawTextInput( 'task_file', $this -> api -> task_file), sprintf( $this -> appCore -> lang['acp_task_form_file_expl'], TASKS_PATH));
		}
		
		// Next Section
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_task_form_sect_runs']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_task_form_run_days'], $this-> appCore -> drawTextInput( 'task_days', $this -> api -> task_days));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_task_form_run_hours'], $this-> appCore -> drawTextInput( 'task_hours', $this -> api -> task_hours));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_task_form_run_minutes'], $this-> appCore -> drawTextInput( 'task_minutes', $this -> api -> task_minutes));
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_task_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['new_task_form'], array(
			$this -> appCore -> adminLink( 'act=tasks') => $this -> appCore -> lang['acp_tasks'],
			'' => $this -> appCore -> lang['new_task_form']
		), ob_get_clean());
			
	}

}